<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Arrays</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PHP, learn PHP, collections, arrays, tutorial, programming language">
<meta name="description" content="In this part of the PHP tutorial, we cover PHP Arrays.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Arrays</h1>

<p>
In this part of the PHP programming tutorial, we will cover arrays. 
We will initialize arrays and read data from them. 
</p>

<p>
<b>Arrays</b> are collections of data. A variable can hold only one item at a time. 
Arrays can hold multiple items. Most programming languages have various types of
collections. Lists, maps, arrays, queues, stacks, tuples etc. PHP has a different
approach. It has one type of collection, an array. Using different syntax, an array
in PHP will behave like a list, an array or a map.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
Technically, a PHP array is a map. It is also called an associative array. 
A map associates values to keys.
</p>

<p>
Arrays are used to store data of our applications.
There are three distinct things we do with arrays. First, we initialize them
with application data. Next, we modify data using assignments or PHP array functions. 
 We have quite a few functions, that
work with arrays. They enable us to modify, sort, merge, slice, shuffle the data inside
the arrays. There are specific database handling functions for populating arrays 
from database queries. Several other functions return arrays. 
Finally, we display data to the console, or in web applications, we display data to the
browser. 
</p>

<h2>Initializing arrays</h2>

<p>
There are several ways, how we can initialize an array in PHP. 
We use the <code>array()</code> function to create an array. In its
simplest form, the function takes an arbitrary number of comma separated values.
</p>


<pre class="code">
&lt;?php

$names = array("Jane", "Lucy", "Timea", "Beky", "Lenka");

print_r($names);

?&gt;
</pre>

<p>
Here we create a <b>$names</b> array. It stores 5 female names.
The <code>print_r()</code> function prints a human readable information
about the variable. 
</p>

<pre>
$ php init1.php 
Array
(
    [0] => Jane
    [1] => Lucy
    [2] => Timea
    [3] => Beky
    [4] => Lenka
)
</pre>

<p>
From the output we can see the names and their indexes by which we
can access them.
</p>

<pre class="code">
&lt;?php

$continents[0] = "America";
$continents[1] = "Africa";
$continents[2] = "Europe";
$continents[3] = "Asia";
$continents[4] = "Antarctica";
$continents[5] = "Australia";

print_r($continents);

?&gt;
</pre>

<p>
This is another way of initializing an array in PHP. 
We create the <b>$continents</b> array by assigning values to
array indexes. "America" has index 0, "Europe" has index 2 etc.
</p>

<pre class="code">
&lt;?php

$continents = array(
    1 => "America", 2 => "Africa",
    3 => "America", 4 => "Asia", 
    5 => "Antarctica");

print_r($continents);

?&gt;
</pre>

<p>
In this example, we create the <b>$continents</b> array with 
the indexes specified. By default, the first index is zero. In
our case, we start with 1.  
</p>

<pre>
$ php init22.php 
Array
(
    [1] => America
    [2] => Africa
    [3] => America
    [4] => Asia
    [5] => Antarctica
)
</pre>

<p>
Now we have an array of continents with indexes, that
we have chosen. 
</p>


<pre class="code">
&lt;?php

$languages[10] = "PHP";
$languages[20] = "Python";
$languages[30] = "Ruby";
$languages[40] = "PERL";
$languages[50] = "Java";

print_r($languages);

?&gt;
</pre>

<p>
The indexes do not have to be consecutive numbers. In our example,
we have chosen numbers 10, 20, 30, 40 and 50 to be the indexes
for the <b>$languages</b> array.
</p>

<pre class="code">
&lt;?php

$actors[] = "Philip Seymour Hoffman";
$actors[] = "Tom Cruise";
$actors[] = "Bill Paxton";
$actors[] = "Adrien Brody";
$actors[] = "Danie Craig";

print_r($actors);

?&gt;
</pre>

<p>
When we do assignment initialization of a PHP array, we can omit
the indexes. The PHP will automatically create the indexes for us.
</p>

<pre>
$ php init4.php 
Array
(
    [0] => Philip Seymour Hoffman
    [1] => Tom Cruise
    [2] => Bill Paxton
    [3] => Adrien Brody
    [4] => Danie Craig
)
</pre>

<p>
The PHP interpreter has created consecutive indexes starting from 
zero. 
</p>


<pre class="code">
&lt;?php

$novels[10] = "Doctor Zhivago"; 
$novels[11] = "War and Peace"; 
$novels[] = "In Cold Blood"; 
$novels[20] = "Crime and Punishment"; 
$novels[] = "Catch XII"; 

print_r($novels);

?&gt;
</pre>

<p>
In this script, we have omitted two indexes. The PHP will 
add them. It will create index 12 and index 21. 
</p>

<pre>
$ php init5.php 
Array
(
    [10] => Doctor Zhivago
    [11] => War and Peace
    [12] => In Cold Blood
    [20] => Crime and Punishment
    [21] => Catch XII
)
</pre>

<p>
PHP has automatically created indexes 12 and 21.
</p>


<pre class="code">
&lt;?php

$countries = array(
    "de" => "Germany", "sk" => "Slovakia",
    "us" => "United States", "ru" => "Russia",
    "hu" => "Hungary", "pl" => "Poland");

print_r($countries);

?&gt;
</pre>

<p>
We create a <b>$countries</b> array. The indexes that we have mentioned
earlier are actually keys in an associative array. The keys may be strings too. 
</p>


<h2>Perusing arrays</h2>

<p>
Next we will read the contents of the arrays. Again, there are several ways how
we can display data from an array. 
</p>

<pre class="code">
&lt;?php

$languages[10] = "PHP";
$languages[20] = "Python";
$languages[30] = "Ruby";
$languages[40] = "PERL";
$languages[50] = "Java";

echo $languages[10], "\n";
echo $languages[20], "\n";
echo $languages[30], "\n";
echo $languages[40], "\n";
echo $languages[50], "\n";

?&gt;
</pre>

<p>
We can access data from an array by their index. 
</p>

<pre>
$ php peruse1.php 
PHP
Python
Ruby
PERL
Java
</pre>

<p>
We have printed all five languages to the console. 
</p>

<pre class="code">
&lt;?php

$continents = array("America", "Africa", "Europe", "Asia", "Antarctica");

$len = count($continents);

for ($i = 0; $i &lt; $len; $i++) {
    echo $continents[$i], "\n";   
}

?&gt;
</pre>

<p>
In this example, we use the <code>for</code> statement to peruse a
<b>$continents</b> array.  
</p>

<pre class="explanation">
 $len = count($continents);
</pre>

<p>
First, we count the number of elements in the array. 
</p>

<pre class="explanation">
for ($i = 0; $i &lt; $len; $i++) {
    echo $continents[$i], "\n";   
}
</pre>

<p>
The <code>for</code> loop prints elements from the
array by indexes 0..$len-1.
</p>



<pre class="code">
&lt;?php

$continents = array("America", "Africa", "Europe", "Asia", "Antarctica");

foreach ($continents as $continent) {
    echo $continent, "\n";    
}

?&gt;
</pre>

<p>
The easiest way to peruse an array is to use the <code>foreach</code>
statement. The statement goes through the array one by one and puts
a current element to the temporary <b>$continent</b> variable. It accesses
data without using their index or key. 
</p>


<pre class="code">
&lt;?php

$countries = array("de" => "Germany", "sk" => "Slovakia",
    "us" => "United States", "ru" => "Russia",
    "hu" => "Hungary", "pl" => "Poland");

function show_values($value, $key)
{
    echo "The $key stands for the $value\n";
}

array_walk($countries, show_values);

?&gt;
</pre>

<p>
In the last example, we use the <code>array_walk()</code> function to peruse an array.
It applies a user function to every member of an array. The user function takes the key and
the value of the item as parameters. 
</p>

<pre>
$ php walk.php 
The de stands for the Germany
The sk stands for the Slovakia
The us stands for the United States
The ru stands for the Russia
The hu stands for the Hungary
The pl stands for the Poland
</pre>

<p>
We print both the key and the value to the console in the sentence.  
</p>

<p>
In this part of the PHP tutorial, we worked with arrays. We initialized the arrays and
read data from them. In the next chapter, we will work with various PHP array functions.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified January 31, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

